package ads_topic

import (
	"server/global"
	"server/model/common/request"
	"server/model/common/response"
	"server/utils"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type AdsTopicApi struct {
}

var adsTopicService = AdsTopicService{}

func (adsTopicApi *AdsTopicApi) CreateAdsTopic(c *gin.Context) {
	var adsTopic AdsTopic
	err := c.ShouldBindJSON(&adsTopic)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	adsTopic.CreatedBy = utils.GetUserID(c)
	verify := utils.Rules{
		"Name": {utils.NotEmpty()},
	}
	if err := utils.Verify(adsTopic, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := adsTopicService.CreateAdsTopic(&adsTopic); err != nil {
		global.Logger.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

func (adsTopicApi *AdsTopicApi) DeleteAdsTopic(c *gin.Context) {
	var adsTopic AdsTopic
	err := c.ShouldBindJSON(&adsTopic)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	adsTopic.DeletedBy = utils.GetUserID(c)
	if err := adsTopicService.DeleteAdsTopic(adsTopic); err != nil {
		global.Logger.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

func (adsTopicApi *AdsTopicApi) DeleteAdsTopicByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	deletedBy := utils.GetUserID(c)
	if err := adsTopicService.DeleteAdsTopicByIds(IDS, deletedBy); err != nil {
		global.Logger.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

func (adsTopicApi *AdsTopicApi) UpdateAdsTopic(c *gin.Context) {
	var adsTopic AdsTopic
	err := c.ShouldBindJSON(&adsTopic)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	adsTopic.UpdatedBy = utils.GetUserID(c)
	verify := utils.Rules{
		"Name": {utils.NotEmpty()},
	}
	if err := utils.Verify(adsTopic, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := adsTopicService.UpdateAdsTopic(adsTopic); err != nil {
		global.Logger.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

func (adsTopicApi *AdsTopicApi) FindAdsTopic(c *gin.Context) {
	var adsTopic AdsTopic
	err := c.ShouldBindQuery(&adsTopic)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if readsTopic, err := adsTopicService.GetAdsTopicById(adsTopic.ID); err != nil {
		global.Logger.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"readsTopic": readsTopic}, c)
	}
}

func (adsTopicApi *AdsTopicApi) GetAdsTopicList(c *gin.Context) {
	var pageInfo AdsTopicSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := adsTopicService.GetAdsTopicInfoList(pageInfo); err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

func (adsTopicApi *AdsTopicApi) GetAllAdsTopics(c *gin.Context) {
	adsTopic, err := adsTopicService.GetAllAdsTopics()
	if err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(gin.H{"adsTopics": adsTopic}, "获取成功", c)
	}
}

func (adsTopicApi *AdsTopicApi) GetAdsTopic(c *gin.Context) {
	adsTopic, err := adsTopicService.GetAdsTopic()
	if err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(gin.H{"adsTopic": adsTopic}, "获取成功", c)
	}
}
